<cfcomponent name="SVGRenderer" namespace="com.nowrists" displayname="SVGRenderer">
	<cfset Variables.Root="d:/inetpub/wwwroot/nowrists">
	<cffunction name="Render" access="remote" returntype="any">
		<cfargument name="SVGFile" type="string" required="true">
		<cfargument name="Format" type="string" required="false" default="png8">
		<cfset var SVGPath=Variables.Root & "/dyn/svg/" & ListLast(ReplaceList(Arguments.SVGFile,"\,:","/,/"),"/") & ".svg">
		<cfset var tfo="">
		<cfset var tfi="">
		<cfset var RenderedOkay=false>
		<cfset var m="">
		<cfif FileExists(SVGPath)>
			<cfset m=CreateObject("component","SVGMunger").init(SVGPath)>
			<!---
			<cfset m.ReplaceIdContent("author","Rick Osborne")>
			<cfset m.ReplaceIdContent("noveltitle","Trust Network, Volume 2")>
			<cfset m.ReplaceIdContent("wordcount","0 words = 0%")>
			--->
			<cfset tfi=GetTempFile(GetTempDirectory(),"svg")>
			<cfset tfo=GetTempFile(GetTempDirectory(),"png")>
			<cfset m.SaveAs(tfi)>
			<cfset RenderedOkay=RenderFile(tfi,tfo)>
			<cftry>
				<cffile action="delete" file="#tfi#">
				<cfcatch></cfcatch>
			</cftry>
			<cfif RenderedOkay>
				<cfcontent file="#tfo#" type="image/png" deletefile="true">
			<cfelse>
				<cftry>
					<cffile action="delete" file="#tfo#">
					<cfcatch></cfcatch>
				</cftry>
				<cfset RenderError("The SVG file could not be rendered.  (This is BAD.)")>
			</cfif>
		<cfelse>
			<cfset RenderError("No such SVG file was found.")>
		</cfif>
	</cffunction>
	<cffunction name="RenderUserInfo" access="remote" returntype="any">
		<cfargument name="UserInfo" type="query" required="true">
		<cfargument name="SVGFile" type="string" required="true">
		<cfargument name="CacheID" type="string" required="false" default="">
		<cfargument name="Format" type="string" required="false" default="png8">
		<cfset var SVGPath=Variables.Root & "/dyn/svg/" & ListLast(ReplaceList(Arguments.SVGFile,"\,:","/,/"),"/") & ".svg">
		<cfset var CachePath=Variables.Root & "/dyn/png/" & Arguments.CacheID & ".png">
		<cfset var tfo="">
		<cfset var tfi="">
		<cfset var RenderedOkay=false>
		<cfset var m="">
		<cfset var ColName="">
		<cfset var DoDelete=true>
		<cfset var FindCache="">
		<cfif UserInfo.RecordCount LT 1>
			<cfset RenderError("No user information was specified.")>
			<cfreturn>
		<cfelseif (UserInfo.RecordCount GT 1)>
			<cfset RenderError("Too much user information was specified.")>
			<cfreturn>
		</cfif>
		<cfif (Arguments.CacheID NEQ "") AND FileExists(CachePath)>
			<cfdirectory action="list" directory="#Variables.Root#/dyn/png/" filter="#Arguments.CacheID#.png" name="FindCache">
			<cfif (FindCache.RecordCount EQ 1) AND IsDate(FindCache.DateLastModified) AND (DateAdd("n",15,FindCache.DateLastModified) GTE Now()) AND (FindCache.Size GT 0)>
				<cfheader name="X-Nano-Cached" value="true">
				<cfcontent file="#CachePath#" type="image/png">
				<cfreturn>
			</cfif>
		</cfif>
		<cfif FileExists(SVGPath)>
			<cfset m=CreateObject("component","SVGMunger").init(SVGPath)>
			<cfloop list="#Arguments.UserInfo.ColumnList#" index="ColName">
				<cfif Right(ColName,1) EQ "_">
					<cfset m.ReplaceIdAttribute(LCase(ListFirst(ColName,"_")),LCase(ListGetAt(ColName,2,"_")),UserInfo[ColName][1])>
				<cfelse>
					<cfset m.ReplaceIdContent(LCase(ColName),UserInfo[ColName][1])>
				</cfif>
			</cfloop>
			<cfset tfi=GetTempFile(GetTempDirectory(),"svg")>
			<cfif (Arguments.CacheID NEQ "")>
				<cfset tfo=CachePath>
				<cfset DoDelete=false>
			<cfelse>
				<cfset tfo=GetTempFile(GetTempDirectory(),"png")>
			</cfif>
			<cfset m.SaveAs(tfi)>
			<cfset RenderedOkay=RenderFile(tfi,tfo)>
			<cftry>
				<cffile action="delete" file="#tfi#">
				<cfcatch></cfcatch>
			</cftry>
			<cfif RenderedOkay>
				<cfcontent file="#tfo#" type="image/png" deletefile="#DoDelete#">
			<cfelse>
				<cfif Arguments.CacheID EQ "">
					<cftry>
						<cffile action="delete" file="#tfo#">
						<cfcatch></cfcatch>
					</cftry>
				</cfif>
				<cfset RenderError("The SVG file could not be rendered.  (This is BAD.)")>
			</cfif>
		<cfelse>
			<cfset RenderError("No such SVG file was found.")>
		</cfif>
	</cffunction>
	<cffunction name="RenderError" access="remote" returntype="any" output="true">
		<cfargument name="Message" type="string" required="true">
		<cfset var m=CreateObject("component","SVGMunger").init(Variables.Root & "/dyn/svg/Error.svg")>
		<cfset var tfi=GetTempFile(GetTempDirectory(),"svg")>
		<cfset var tfo=GetTempFile(GetTempDirectory(),"png")>
		<cfset var DoDelete=true>
		<cfset m.ReplaceIdContent("msg",Arguments.Message)>
		<cfset m.SaveAs(tfi)>
		<cfif NOT RenderFile(tfi,tfo)>
			<cfset tfo=Variables.Root & "/images/blank.png">
			<cfset DoDelete=false>
		</cfif>
		<cftry>
			<cffile action="delete" file="#tfi#">
			<cfcatch></cfcatch>
		</cftry>
		<cfcontent file="#tfo#" type="image/png" deletefile="#DoDelete#">
		<cfreturn>
	</cffunction>
	<cffunction name="RenderFile" access="public" returntype="boolean" output="true">
		<cfargument name="InFile" type="string" required="true">
		<cfargument name="OutFile" type="string" required="true">
		<cfargument name="Width" type="string" required="false" default="">
		<cfargument name="Format" type="string" required="false" default="png8">
		<cfset var tc="">
		<cfset var in="">
		<cfset var out="">
		<cfset var uri=CreateObject("java", "java.io.File").init(Arguments.InFile).toURL().toString()>
		<cfset var os=CreateObject("java", "java.io.FileOutputStream").init(Arguments.OutFile)>
		<cfset var isPNG=false>
		<cfset var png="">
		<cfif Left(Arguments.Format,4) EQ "jpeg">
			<cfset tc=CreateObject("java","org.apache.batik.transcoder.image.JPEGTranscoder").init()>
			<cfif (Len(Arguments.Format) GT 4) AND IsNumeric(Mid(Arguments.Format,5,2))>
				<cfset tc.addTranscodingHint(tc.KEY_QUALITY,JavaCast("float",Val(Mid(Arguments.Format,5,2))/100))>
			</cfif>
		<cfelse>
			<cfset isPNG=true>
			<cfset tc=CreateObject("java","org.apache.batik.transcoder.image.PNGTranscoder").init()>
		</cfif>
		<cfif IsNumeric(Arguments.Width) AND (Arguments.Width GTE 10) AND (Arguments.Width LTE 640)>
			<cfset tc.addTranscodingHint(tc.KEY_WIDTH,JavaCast("float",Arguments.Width))>
		</cfif>
		<cfset in=CreateObject("java", "org.apache.batik.transcoder.TranscoderInput").init(uri)>
		<cfset out=CreateObject("java", "org.apache.batik.transcoder.TranscoderOutput").init(os)>
		<cftry>
			<cfset tc.transcode(in,out)>
			<cfcatch>
				<cfdump var="#CFCatch#">
				<cfabort>
				<cfreturn false>
			</cfcatch>
		</cftry>
		<cfset os.flush()>
		<cfset os.close()>
		<cfif isPNG>
			<cftry>
				<cfset png=createObject("component","com.nowrists.PNGManip").init(javaCast("string",arguments.OutFile))>
				<cfset png.removeChunkTypes("gAMA,tEXt,iTXt,zTXt,iCCP,sRGB,cHRM,pHYs,sPLT,hIST,tIME,oFFs,pCAL,sCAL,gIFg,gIFx,sTER,fRAc,gIFt,mkBF,mkTS,mkBT,prVW,mkBS")>
				<cfset png.updateTime()>
				<cfset png.consolidateDataChunks()>
				<cfset png.appendTextChunk("nowrists.com","Author")>
				<cfset png.writePNG(arguments.OutFile)>
			<cfcatch></cfcatch>
			</cftry>
		</cfif>
		<cfreturn true>
	</cffunction>
</cfcomponent>